package zhtt.biz.web.controller;

import com.gitee.zhtt.entity.JsonResponse;
import org.springframework.web.bind.annotation.*;
import zhtt.biz.web.entity.PageInfo;
import zhtt.biz.web.entity.User;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    /**
     * 根据ID查询详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public JsonResponse get(@PathParam("id") String id) {
        User user = new User();
        return JsonResponse.success(user);
    }

    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResponse delete(@PathParam("id") String id) {
        User user = new User();
        return JsonResponse.success("删除成功！");
    }

    /**
     * 保存数据
     * @param user
     * @return
     */
    @PostMapping
    public JsonResponse save(@RequestBody User user) {
        return JsonResponse.success("保存成功！");
    }

    /**
     * 更新数据
     * @param user
     * @return
     */
    @PutMapping
    public JsonResponse update(@RequestBody User user) {
        return JsonResponse.success("更新成功！");
    }

    /**
     * 条件查询，分页返回数据
     * @param current
     * @param size
     * @param keyword
     * @return
     */
    @GetMapping
    public JsonResponse query(@RequestParam(name = "current", defaultValue = "1")Integer current,
                              @RequestParam(name = "size", defaultValue = "20")Integer size,
                              @RequestParam(name = "keyword", required = false)String keyword) {
        List<User> userList = new ArrayList<User>();
        for(int i=0;i<5;i++){
            User user = new User();
            userList.add(user);
        }
        return JsonResponse.success(new PageInfo<User>(current, size, 20, userList));
    }

    @GetMapping("/map/test")
    public Map<String,Object> map(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("resultCode",-1);
        map.put("resultMsg","操作失败，请稍后重试！");
        return map;
    }
}
